package net.gz01.wallet.web.controller.admin;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

import net.gz01.util.ZODate;
import net.gz01.wallet.common.PageRespon;
import net.gz01.wallet.common.TopBank;
import net.gz01.wallet.common.ZOIDGenerate;
import net.gz01.wallet.common.ZOSearchCondition;
import net.gz01.wallet.web.controller.BaseController;
import net.gz01.wallet.web.model.Account;
import net.gz01.wallet.web.model.YouliAccount;

public class YouliAccountController extends BaseController{

	public void search(){
		render("basedata/youliAccountList.html");
	}

	public void listJson() {
		ZOSearchCondition sc = super.getSearchCondition();
		Page<YouliAccount> pageResult = YouliAccount.dao.search(sc);
		renderJson(new PageRespon(pageResult));
	}

	public void addPage(){
		setAttr("topBankList", TopBank.topBankList);
		render("basedata/youliAccountAdd.html");
	}

	public void updatePage(){
		YouliAccount youliAccount = YouliAccount.dao.findById(getPara("id"));
		setAttr("youliAccount", youliAccount);
		setAttr("topBankList", TopBank.topBankList);
		render("basedata/youliAccountUpdate.html");
	}

	@Before(Tx.class)
	public void add(){
		YouliAccount youliAccount = getModel(YouliAccount.class);
		if(youliAccount.getBankEnglishLetters() == null){
			youliAccount.setBankEnglishLetters("999999");
		}else {
			youliAccount.setBankName(TopBank.getByShortEnName(youliAccount.getBankEnglishLetters()).getBankName());
		}
		youliAccount.save();

		Account ac = new Account();
		ac.setAccountNo("T" + youliAccount.getAccountNo());//Temp代表暂存
		ac.setAccountName(youliAccount.getAccountName());
		ac.setBalance(0L);
		ac.setFreezeAmount(0L);
		ac.setEnable(true);
		ac.setCreateDateTime(ZODate.getDateTime());
		ac.setTemp(true);
		ac.save();
		renderSuccess();
	}

	@Before(Tx.class)
	public void update(){
		YouliAccount youliAccount = getModel(YouliAccount.class);
		youliAccount.update();

		Account temp = Account.dao.select(("T" +youliAccount.getAccountNo()));
		if(null == temp) {
			Account ac = new Account();
			ac.setAccountNo("T" + youliAccount.getAccountNo());//Temp代表暂存
			ac.setAccountName(youliAccount.getAccountName());
			ac.setBalance(0L);
			ac.setFreezeAmount(0L);
			ac.setEnable(true);
			ac.setCreateDateTime(ZODate.getDateTime());
			ac.setTemp(true);
			ac.save();
		}else {
			temp.setAccountNo("T" + youliAccount.getAccountNo());//Temp代表暂存
			temp.setAccountName(youliAccount.getAccountName());
			temp.update();
		}
		renderSuccess();
	}

	@Before(Tx.class)
	public void delete(){
		YouliAccount.dao.deleteById(getPara("id"));
		renderSuccess(false);
	}

	public void autocomplete() {
		ZOSearchCondition sc = super.getSearchCondition();
		String keywords = sc.getStr("keywords");
		List<YouliAccount> accountList = YouliAccount.dao.autocomplete(keywords);
		JSONObject j = new JSONObject();
		j.put("type", "success");
		j.put("code", "0");
		j.put("content", accountList);
		renderJson(j);
	}

	public void autocompleteBank(){
		List<TopBank> bankList = new ArrayList<>();
		ZOSearchCondition sc = super.getSearchCondition();
		String keywords = sc.getStr("keywords");
		for (int i = 0; i < TopBank.topBankList.size(); i++) {
			if (TopBank.topBankList.get(i).getBankName().contains(keywords)) {
				bankList.add(TopBank.topBankList.get(i));
			}
		}
		JSONObject j = new JSONObject();
		j.put("type", "success");
		j.put("code", "0");
		j.put("content", bankList);
		renderJson(j);
	}


}
